﻿using AutoMapper;
using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Contract.ContractMes;
using HamcoWcs.DAL;
using HamcoWcs.Entitys.MesMsgQueues;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.LcdInfos;
using HamcoWcs.Service.Common;
using System;
using System.Collections.Generic;
using System.Xml;

namespace HamcoWcs.Service.Mes.Parsers
{
    public class MesMsgParser_CurrentStockerEmptyBoxPlanRequest : MesMsgParserBase, IMesMsgParser
    {
        private readonly IBaseConfigService _config;
        public MesMsgParser_CurrentStockerEmptyBoxPlanRequest(IBaseConfigService config, ILoggerService loggerService, WcsDbContext wcsDbContext, IMapper mapper)
            : base(loggerService, wcsDbContext, mapper)
        {
            _config = config;
        }

        public override string EventName => "CurrentStockerEmptyBoxPlanRequest";


        public override void HandleEvent(MesMsgQueue message)
        {
            var xml = message.Return;
            var doc = new XmlDocument();
            doc.LoadXml(xml);

            var machineName = doc.SelectSingleNode("//MACHINENAME")?.InnerText;
            var retunCode = doc.SelectSingleNode("//RETURNCODE")?.InnerText ?? "0";

            if (string.IsNullOrEmpty(machineName))
            {
                message.Status = (int)EnumMesMsgStatus.Completed;
                return;
            }


            if (retunCode != "0")
            {
                message.Status = (int)EnumMesMsgStatus.Completed;
                return;
            }


            var plans = doc.SelectNodes("//PLAN");

            var aa = StockerPlanInfo.StockerPlanDic.TryGetValue(machineName,out Dictionary<string, StockerPlanInfo> dic);
            if (!aa)
            {
                message.Status = (int)EnumMesMsgStatus.Completed;
                return;
            }

            foreach (var info in dic.Values)
            {
                info.CurrentQty = 0;
                info.PlanQty = 0;
                info.InputQty = 0;
            }

            for (int i = 0; i < plans.Count; i++)
            {
                var plan = plans[i];

                var settingCode = plan.SelectSingleNode("./SETTINGCODE")?.InnerText;
                if (string.IsNullOrEmpty(settingCode)) continue;

                var planQty = plan.SelectSingleNode("./PLANQUANTITY")?.InnerText ?? "0";
                var inputQty = plan.SelectSingleNode("./INPUTQUANTITY")?.InnerText ?? "0";
                var currentQty = plan.SelectSingleNode("./CURRENTQUANTITY")?.InnerText ?? "0";

                var size = GetSizeBySettingCode(settingCode);

                if (size == "UNK") continue;

                var a = dic.TryGetValue(size, out StockerPlanInfo info);

                if (!a) continue;

                info.CurrentQty += currentQty.ToInt();
                info.PlanQty += planQty.ToInt();
                info.InputQty += inputQty.ToInt();
            }

            message.Status = (int)EnumMesMsgStatus.Completed;
        }




        /// <summary>
        /// 按SettingCode获取尺寸<br/>
        /// Code-尺寸在配置表中配置
        /// </summary>
        /// <param name="settingCode"></param>
        /// <returns></returns>
        private string GetSizeBySettingCode(string settingCode)
        {
            var k = $"SETTINGCODE{settingCode}";
            var a = _config.TryGetConfigValue(k, out string size);
            if (a) return size;
            return "UNK";
        }
    }
}
